package cn.tansanqinger.service.topic.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.ICommentTopicSqlDao;
import cn.tansanqinger.dao.sql.IDisposeresultSqlDao;
import cn.tansanqinger.dao.sql.account.UserAccountSqlDaoImpl;
import cn.tansanqinger.dao.sql.group.ReportUserAndGroupSqlDaoImpl;
import cn.tansanqinger.dao.sql.topic.TopicSqlDaoImpl;
import cn.tansanqinger.pojo.Admin;
import cn.tansanqinger.pojo.Commenttopic;
import cn.tansanqinger.pojo.Disposeresult;
import cn.tansanqinger.pojo.Reportuser;
import cn.tansanqinger.pojo.Topic;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.topic.ITopicService;
import cn.tansanqinger.util.DateUtil;
import cn.tansanqinger.util.Tools;

@Service
public class TopicServiceImpl implements ITopicService{

	@Autowired
	private TopicSqlDaoImpl impl;
	@Autowired
	private ReportUserAndGroupSqlDaoImpl rimpl;
	@Autowired
	private IDisposeresultSqlDao ddao;
	@Autowired
	private ICommentTopicSqlDao cdao;
	@Autowired
	private UserAccountSqlDaoImpl uimpl;

	@Override
	public Map<String, Object> selectMessage(HttpServletRequest request) {
		String keywords = Tools.isEmpty(request.getParameter("keywords"));
		String option = Tools.isEmpty(request.getParameter("option"));
		String page = Tools.isEmpty(request.getParameter("page"));
		String limit = Tools.isEmpty(request.getParameter("limit"));
		int kispopupInt = -1;
		int pageInt = 1;
		if(!"".equals(page)) {
			pageInt = Integer.parseInt(page);
		}
		int limitInt = 10;
		if(!"".equals(limit)) {
			limitInt = Integer.parseInt(limit);
		}
		return impl.selectMessage(keywords, kispopupInt, Integer.parseInt(option), pageInt, limitInt);
	}

	@Override
	public Map<String, Object> serialNumber() {
		Integer csn = impl.serialNumber();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code","0");
		map.put("msg", csn);
		return map;
	}

	@Override
	public Map<String, Object> add(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Topic topic = new Topic();
		Admin admin = (Admin) request.getSession().getAttribute("userMessage");
		topic.setTsn(impl.serialNumber());
		topic.setTmessage(Tools.isEmpty(request.getParameter("tmessage")));
		topic.setTispopup(0);
		topic.setTisquery(0);
		topic.setTauid(admin.getAid());
		topic.setTnumber(0);
		topic.setTcollectnnumber(0);
		topic.setTcomment(0);
		topic.setTaudit(1);
		topic.setTisreport(0);
		topic.setTtype(Integer.parseInt(Tools.isEmpty(request.getParameter("type"))));
		if(topic.getTtype()==1) {
			String answers[] = Tools.isEmpty(request.getParameter("answer")).split(",");
			Arrays.sort(answers);
			String answer = "";
			for (int i = 0; i < answers.length; i++) {
				answer+=answers[i];
				if(i<answers.length-1) {
					answer+=",";
				}
			}
			topic.setTanswer(answer);
		} else {
			topic.setTanswer(Tools.isEmpty(request.getParameter("answer")));
		}
		topic.setTanswerall(Tools.isEmpty(request.getParameter("answerall")));
		topic.setTdifficulty(Integer.parseInt(Tools.isEmpty(request.getParameter("difficulty"))));
		topic.setTgrade(Integer.parseInt(Tools.isEmpty(request.getParameter("grade"))));
		topic.setTclassify(Integer.parseInt(Tools.isEmpty(request.getParameter("classify"))));
		topic.setTtime(DateUtil.getTime());
		topic.setTqtime(DateUtil.getTime());
		if(impl.add(topic)==1) {
			resultMap.put("code","0");
			resultMap.put("msg", "添加成功");
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "添加失败");
		}
		return resultMap;	
	}

	@Override
	public Map<String, Object> updateStatus(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(id.matches("\\d+")) {
			Topic object = impl.selectOnt(Integer.parseInt(id));
			object.setTispopup(object.getTispopup()==0?1:0);
			if(impl.updateStatus(object)==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "修改成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "修改失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据异常");
		}
		return resultMap;
	}

	@Override
	public Topic selectIdOne(String id) {
		return impl.selectOnt(Integer.parseInt(id));
	}

	@Override
	public Map<String, Object> delete(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(id.matches("\\d+")) {
			if(impl.deleteOne(Integer.parseInt(id))==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "删除成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "删除失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据错误");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> modification(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Topic topic = selectIdOne(Tools.isEmpty(request.getParameter("id")));
		topic.setTmessage(Tools.isEmpty(request.getParameter("tmessage")));
		if(topic.getTtype()==1) {
			String answers[] = Tools.isEmpty(request.getParameter("answer")).split(",");
			Arrays.sort(answers);
			String answer = "";
			for (int i = 0; i < answers.length; i++) {
				answer+=answers[i];
				if(i<answers.length-1) {
					answer+=",";
				}
			}
			topic.setTanswer(answer);
		} else {
			topic.setTanswer(Tools.isEmpty(request.getParameter("answer")));
		}
		topic.setTanswerall(Tools.isEmpty(request.getParameter("answerall")));
		topic.setTdifficulty(Integer.parseInt(Tools.isEmpty(request.getParameter("difficulty"))));
		topic.setTgrade(Integer.parseInt(Tools.isEmpty(request.getParameter("grade"))));
		topic.setTclassify(Integer.parseInt(Tools.isEmpty(request.getParameter("classify"))));
		topic.setTqtime(DateUtil.getTime());
		if(impl.modification(topic)==1) {
			resultMap.put("code","0");
			resultMap.put("msg", "修改成功");
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "修改失败");
		}
		return resultMap;	
	}
	
	@Override
	public Map<String, Object> modification2(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Topic topic = selectIdOne(Tools.isEmpty(request.getParameter("id")));
		topic.setTaudit(1);
		if("1".equals(Tools.isEmpty(request.getParameter("tispopup")))) {
			topic.setTispopup(1);
		} else {
			topic.setTispopup(0);
		}
		if(impl.modification(topic)==1) {
			resultMap.put("code","0");
			resultMap.put("msg", "处理成功");
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "处理失败");
		}
		return resultMap;	
	}

	@Override
	public Map<String, Object> selectReportComment(HttpServletRequest request) {
		String time = Tools.isEmpty(request.getParameter("time"));
		String page = Tools.isEmpty(request.getParameter("page"));
		String limit = Tools.isEmpty(request.getParameter("limit"));
		int pageInt = 1;
		if(!"".equals(page)) {
			pageInt = Integer.parseInt(page);
		}
		int limitInt = 10;
		if(!"".equals(limit)) {
			limitInt = Integer.parseInt(limit);
		}
		long startTime = -1;
		long endTime = -1;
		if(!"".equals(time)) {
			startTime = DateUtil.getTime2(time.substring(0,10))/1000;
			endTime = DateUtil.getTime2(time.substring(13,23))/1000;
		}
		return rimpl.selectReportComment(startTime, endTime, pageInt, limitInt);
	}

	@Override
	public Map<String, Object> deleteComment(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			Reportuser reportuser = rimpl.selectReportuserOne(Integer.parseInt(id));
			if(reportuser!=null) {
				Commenttopic commenttopic = cdao.selectById(reportuser.getIid());
				if(commenttopic!=null) {//不等于空
					Disposeresult disposeresult = new Disposeresult();
					//被举报者反馈
					disposeresult.setUid(commenttopic.getUid());
					disposeresult.setOid(reportuser.getRid());
					disposeresult.setDtype(5);
					disposeresult.setDmessage("因为你的言论不当，管理员将你发布的评论“"+commenttopic.getCmessage()+"”进行了删除，请您保存良好的网络环境。");
					disposeresult.setIsselect(0);
					disposeresult.setDtime(System.currentTimeMillis()/1000);
					disposeresult.setDdate(DateUtil.getTime());
					ddao.insert(disposeresult);
					//给举报者反馈
					disposeresult = new Disposeresult();
					disposeresult.setUid(reportuser.getUid());
					disposeresult.setOid(reportuser.getRid());
					disposeresult.setDtype(5);
					User user = uimpl.selectId(commenttopic.getUid());
					if(user!=null) {
						disposeresult.setDmessage("管理员已经将你举报的用户《"+("".equals(user.getName())?user.getAccount():user.getName())+"》发表的评论“"+commenttopic.getCmessage()+"”进行了删除，感谢你为良好的网络环境做出共享。");
					} else {
						disposeresult.setDmessage("管理员已经将你举报的不良言论进行了删除，感谢你为良好的网络环境做出共享。");
					}
					disposeresult.setIsselect(0);
					disposeresult.setDtime(System.currentTimeMillis()/1000);
					disposeresult.setDdate(DateUtil.getTime());
					ddao.insert(disposeresult);
					cdao.deleteById(commenttopic.getCid());
				}
				reportuser.setRisdispose(1);
				if(rimpl.modification(reportuser)==1) {
					resultMap.put("code","0");
					resultMap.put("msg", "处理成功");
				} else {
					resultMap.put("code","-1");
					resultMap.put("msg", "处理失败");
				}
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "数据异常");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据异常");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> ignoreComment(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			Reportuser reportuser = rimpl.selectReportuserOne(Integer.parseInt(id));
			if(reportuser!=null) {
				reportuser.setRisdispose(1);
				if(rimpl.modification(reportuser)==1) {
					resultMap.put("code","0");
					resultMap.put("msg", "处理成功");
				} else {
					resultMap.put("code","-1");
					resultMap.put("msg", "处理失败");
				}
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "数据异常");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据异常");
		}
		return resultMap;
	}
}
